package com.jingtai.platform.service.supply.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.common.utils.random.TokenUtil;
import com.jingtai.core.entity.supply.SupplyGoodsWarehouse;
import com.jingtai.core.mapper.supply.SupplyGoodsWarehouseMapper;
import com.jingtai.platform.service.shop.GoodsSpecificationsService;
import com.jingtai.platform.service.supply.SupplyGoodsWarehouseService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * 供应商商品属性表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-02 18:31:32
 */
@Service
public class SupplyGoodsWarehouseServiceImpl extends ServiceImpl<SupplyGoodsWarehouseMapper, SupplyGoodsWarehouse> implements SupplyGoodsWarehouseService {

    @Autowired
    private SupplyGoodsWarehouseService supplyGoodsWarehouseService;
    @Autowired
    private GoodsSpecificationsService goodsSpecificationsService;

    @Override
    public ResultUtil SupplyGoodsWarehouselist(Integer page, Integer limit, String conditions){
        QueryWrapper<SupplyGoodsWarehouse> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,supplyGoodsWarehouseService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addSupplyGoodsWarehouse(SupplyGoodsWarehouse supplyGoodsWarehouse) {
        supplyGoodsWarehouse.setSkuCode(TokenUtil.generateOrderNo());
        //格式化 属性json
        supplyGoodsWarehouse.setSpecificationsJson(goodsSpecificationsService.formatJson(supplyGoodsWarehouse.getSpecificationsJson(), supplyGoodsWarehouse.getGoodsCode(),0));
        boolean save = supplyGoodsWarehouseService.save(supplyGoodsWarehouse);
        return new ResultUtil(save ? ResultMapInfo.ADDSUCCESS:ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getSupplyGoodsWarehouseById(Long id) {
        SupplyGoodsWarehouse supplyGoodsWarehouse = supplyGoodsWarehouseService.getById(id);
        return supplyGoodsWarehouse == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,supplyGoodsWarehouse);
    }

    @Override
    public ResultUtil delSupplyGoodsWarehouseById(Long id) {
        SupplyGoodsWarehouse supplyGoodsWarehouse = new SupplyGoodsWarehouse();
        supplyGoodsWarehouse.setId(id);
        supplyGoodsWarehouse.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = supplyGoodsWarehouseService.updateById(supplyGoodsWarehouse);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil getSupplyGoodsWarehouseByGoodsCode(String goodsCode) {
        List<SupplyGoodsWarehouse> list = supplyGoodsWarehouseService.list(Wrappers.lambdaQuery(SupplyGoodsWarehouse.class).eq(SupplyGoodsWarehouse::getGoodsCode, goodsCode).eq(SupplyGoodsWarehouse::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        return new ResultUtil(ResultMapInfo.GETSUCCESS,list);
    }

    @Override
    public ResultUtil editSupplyGoodsWarehouse(SupplyGoodsWarehouse supplyGoodsWarehouse) {
        supplyGoodsWarehouse.setSpecificationsJson(goodsSpecificationsService.formatJson(supplyGoodsWarehouse.getSpecificationsJson(), supplyGoodsWarehouse.getGoodsCode(),0));
        boolean editState = supplyGoodsWarehouseService.updateById(supplyGoodsWarehouse);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
